import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
//import Login from '../views/login/Login.vue'
// import test from '../views/test/Index.vue'
import { queryMenu } from '@/network/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/Login.vue')
  },
  {
    path: '/404',
    name: '4041',
    component: () => import('@/views/404.vue')
  },
  {
    path: '/',
    name: 'Index',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/index/Index.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('@/components/Welcome.vue')
      },
      {
        path: '/homepage',
        component: () => '../views'
      },
      {
        path: '/bookson',
        component: () => import('../views/booxmanager/BooksOn.vue')
      },
      {
        path: '/booksquery',
        component: () => import('../views/booxmanager/BooksQuery.vue')
      },
      {
        path: '/classificationquery',
        component: () =>
          import('../views/classificationmanager/classificationquery.vue')
      },
      {
        path: '/imagequery',
        component: () => import('@/views/imageManager/imagequery.vue')
      },
      {
        path: '/chapteron',
        component: () => import('@/views/chapterManager/chapteron.vue')
      },
      {
        path: '/onlineedit',
        component: () => import('@/components/OnLineEdit.vue')
      },
      {
        path: '/chapterquery',
        component: () => import('@/views/chapterManager/chapterquery.vue')
      },
      {
        path: '/chapterdetails/:novelId',
        name: 'chapterDetails',
        component: () => import('@/components/chapterDetails.vue')
      },
      {
        path: '/searchquery',
        name: 'chapterDetails',
        component: () => import('@/views/searchHistory/querySearchHistory.vue')
      },
      {
        path: '/booksdustbin',
        name: 'booksDustbin',
        component: () => import('@/views/booxmanager/booksdustbin.vue')
      },
      {
        path: '/chapterdustbin',
        name: 'chapterDustbin',
        component: () => import('@/views/chapterManager/chapterdustbin.vue')
      },
      {
        path: '/classificationdustbin',
        name: 'classificationDustbin',
        component: () =>
          import('@/views/classificationmanager/classificationdustbin.vue')
      },
      {
        path: '/imagedustbin',
        name: 'imageDustbin',
        component: () => import('@/views/imageManager/imagedustbin.vue')
      },
      {
        path: '/queryuser',
        name: 'queryUser',
        component: () => import('@/views/userManager/queryUser.vue')
      },
      {
        path: '/permissionsetting',
        name: 'permissionSetting',
        component: () =>
          import('@/views/authorityManager/permissionSetting.vue')
      },
      {
        path: '/rolesetting',
        name: 'roleSetting',
        component: () => import('@/views/authorityManager/roleSetting.vue')
      },
   
    ]
  },

  {
    path: '*',
    name: '404',
    component: () => import('@/views/404.vue')
  }
]

const router = new VueRouter({
  routes
})
// 挂在路由导航守卫
router.beforeEach(async (to, from, next) => {
  // console.info(store.getters.roles.length === 0)
  // to将要访问的页面
  // from 代表从哪个页面来
  // next 是个函数，表示放行
  // next() 放行  next('/login') 强制跳转
  if (to.path === '/login') return next()
  // 获取user
  const user = sessionStorage.getItem('user')
  if (!user) return next('/login')
  //获取用户菜单栏
  let menuList = sessionStorage.getItem('menuList')
  if (!menuList) {
    let res = await queryMenu(JSON.parse(user))
    // 缓存角色信息
    console.info(res)
    sessionStorage.setItem('role',JSON.stringify(JSON.parse(res.resp_data).role))
    menuList=JSON.parse(res.resp_data).list
    sessionStorage.setItem('menuList', JSON.stringify(JSON.parse(res.resp_data).list)||[])
  }
  console.info(menuList)
  console.info(to.path)
  if(menuList.length==0){
   return next('/welcome')
  }
  if(to.path=='/welcome'||'/chapterDetails'==to.path||'/OnLineEdit'==to.path){
    return next()
  }
  for (let ml =0; ml< menuList.length;ml++) {
    for (let m in menuList[ml].menuList) {
      if ('/' + menuList[ml].menuList[m].menuUrl == to.path) {
        return next()
      }
    }
  }
 return next()
})

export default router
